
# 编译配置
CC = gcc
CFLAGS = -Wall -Wextra -g
SERVER_LDFLAGS = -pthread
CLIENT_DIR = client
SERVER_DIR = server
OBJ_DIR = obj
BIN_DIR = bin

# 客户端构建配置
CLIENT_TARGET = $(BIN_DIR)/client
CLIENT_SOURCES = $(wildcard $(CLIENT_DIR)/*.c)
CLIENT_OBJS = $(patsubst $(CLIENT_DIR)/%.c,$(OBJ_DIR)/client_%.o,$(CLIENT_SOURCES))


# 服务器端构建配置
SERVER_TARGET = $(BIN_DIR)/server
SERVER_SOURCES = $(wildcard $(SERVER_DIR)/*.c)
SERVER_OBJS = $(patsubst $(SERVER_DIR)/%.c,$(OBJ_DIR)/server_%.o,$(SERVER_SOURCES))

# 主构建目标
all: create_dirs $(SERVER_TARGET) $(CLIENT_TARGET)

# 创建必要目录
create_dirs:
	@mkdir -p $(OBJ_DIR) $(BIN_DIR) logs


# 客户端构建规则
$(CLIENT_TARGET): $(CLIENT_OBJS)
	$(CC) $(CFLAGS) $^ -o $@

$(OBJ_DIR)/client_%.o: $(CLIENT_DIR)/%.c
	$(CC) $(CFLAGS) -I$(CLIENT_DIR) -c $< -o $@


# 服务器端构建规则
$(SERVER_TARGET): $(SERVER_OBJS)
	$(CC) $(CFLAGS) $(SERVER_LDFLAGS) $^ -o $@

$(OBJ_DIR)/server_%.o: $(SERVER_DIR)/%.c
	$(CC) $(CFLAGS) -I$(SERVER_DIR) -c $< -o $@


# 清理规则
clean:
	rm -rf $(OBJ_DIR) $(BIN_DIR)

.PHONY: all create_dirs clean